package com.primb.androidlibs.override;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.primb.androidlibs.R;
import com.primb.androidlibs.ui.ArrowTextView;
import com.primb.androidlibs.utils.FormatUtils;

/**
 * Created by JKWANG-PC on 2016/11/1.
 * <p>
 * Since release v3.0.0, markers (popup views) in the chart are represented by the IMarker interface.
 */

public class MPChartMarkerView extends MarkerView {

    private ArrowTextView tvContent;

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MPChartMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        tvContent = (ArrowTextView) findViewById(R.id.tvContent);
        tvContent.setBgColor(Color.parseColor("#e9b0d4"));
        tvContent.setTextColor(Color.WHITE);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        if (e instanceof CandleEntry) {
            CandleEntry ce = (CandleEntry) e;
            tvContent.setText(FormatUtils.format(ce.getHigh(), 2) + "");
        } else if (e instanceof BarEntry) {
            float[] yVals = ((BarEntry) e).getYVals();
            if (yVals == null) {
                tvContent.setText(FormatUtils.format(e.getY(), 2) + "");
            } else if (highlight.getStackIndex() == -1) {
                tvContent.setText(FormatUtils.format(highlight.getY(), 2) + "");
            } else {
                tvContent.setText(FormatUtils.format(yVals[highlight.getStackIndex()], 2) + "");
            }
        } else {
            tvContent.setText(FormatUtils.format(e.getY(), 2) + "");
        }
        super.refreshContent(e, highlight);//必须加上该句话；
    }

    private MPPointF mOffset;

    @Override
    public MPPointF getOffset() {
        if (mOffset == null) {
            // center the marker horizontally and vertically
            mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
        }
        return mOffset;
    }
}
